package com.ant.storage.controller;


import com.ant.storage.common.PageRequest;
import com.ant.storage.common.ResultCode;
import com.ant.storage.common.ResultPage;
import com.ant.storage.common.ResultVo;
import com.ant.storage.dto.ImgUrlDto;
import com.ant.storage.dto.SupplierInformationDto;
import com.ant.storage.dto.SupplierOrderDto;
import com.ant.storage.dto.TimeDto;
import com.ant.storage.pojo.SupplierInformation;
import com.ant.storage.service.ISupplierInformationService;
import com.ant.storage.vo.SupplierDetailsVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author melon
 * @since 2022-03-16
 */
@RestController
@RequestMapping("/supplierInformation")
@Api(tags = "供应商信息接口")
public class SupplierInformationController {

    @Autowired
    private ISupplierInformationService supplierInformationService;

    @ApiOperation(value = "获取供应商信息数据分页列表")
    @GetMapping("/getSupplierInformation")
    public ResultPage getSupplierInformation(SupplierInformationDto supplierInformationDto) {
        return ResultPage.success("获取成功", supplierInformationService.queryAllByPage(supplierInformationDto));
    }

    @ApiOperation(value = "获取供应商信息数据不分页列表")
    @GetMapping("/getSupplierInformationList")
    public ResultVo getSupplierInformationList() {
        List<SupplierInformation> list =
                supplierInformationService.list(new QueryWrapper<SupplierInformation>().eq("is_show", 1));
        return ResultVo.success("获取成功",list);
    }


    @ApiOperation(value = "获取供应商信息数据不分页列表")
    @GetMapping("/getSupplierInformationTypeList/{typeId}")
    public ResultVo getSupplierInformationTypeList(@PathVariable("typeId") Integer typeId) {
        List<SupplierInformation> list =
                supplierInformationService.list(new QueryWrapper<SupplierInformation>().eq("supplier_type", typeId).eq("is_show", 1));
        return ResultVo.success("获取成功",list);
    }


    @ApiOperation(value = "添加供应商信息审核")
    @PostMapping("/addSupplierInformation")
    public ResultVo addSupplierInformation(@RequestBody @Valid SupplierInformation supplierInformation, BindingResult bindingResult) {
        return supplierInformationService.addSupplierInformation(supplierInformation, bindingResult);
    }

    @ApiOperation(value = "通过id查询供应商采购及结算信息")
    @GetMapping("/findPaymenListById")
    public ResultPage findPaymenListById(SupplierInformationDto dto) {
        if (dto.getId() == null) {
            return ResultPage.fail(ResultCode.PARAM_IS_BLANK);
        } else {
           return ResultPage.success("查询采购",supplierInformationService.findPaymenList(dto));
        }
    }

    @ApiOperation(value = "通过id查询供应商采购新分页")
    @GetMapping("/findPaymenPageListByIds")
    public ResultPage findPaymenPageListByIds(SupplierOrderDto dto) {
        if (dto.getId() == null) {
            return ResultPage.fail(ResultCode.PARAM_IS_BLANK);
        } else {
            return ResultPage.success("查询采购",supplierInformationService.findPaymenLists(dto));
        }
    }

    @ApiOperation(value = "财务通过id查看月结申请明细")
    @GetMapping("/getMonthlySettlementById/{id}")
    private ResultVo getMonthlySettlementById(PageRequest pageRequest,
                                              @PathVariable(value = "id", required = false) Integer id) {
        if (id == null) {
            return ResultVo.fail("id不能为空");
        } else {
            return supplierInformationService.getMonthlySettlementById(pageRequest, id);
        }
    }

    @ApiOperation(value = "修改票据")
    @PostMapping("/updateImgUrl")
    public ResultVo updateImgUrl(@RequestBody ImgUrlDto dto) {
        return supplierInformationService.updateImgUrl(dto);
    }


    @ApiOperation(value = "出入详情")
    @GetMapping(value = "/details")
    public ResultVo details(TimeDto dto) {
        List<SupplierDetailsVo> details = supplierInformationService.details(dto);
        return ResultVo.success("查询采购",details);
    }


    @ApiOperation(value = "删除")
    @PutMapping("/del/{id}")
    public ResultVo delPickingById(@PathVariable("id") Integer id) {
        supplierInformationService.update(new UpdateWrapper<SupplierInformation>().set("is_show", 0).eq("id", id));
        return ResultVo.success("操作成功");
    }

}
